package com.online.demo.controller;


import com.online.demo.common.ResultBean;
import com.online.demo.entity.TContent;
import com.online.demo.service.IFastDFSService;
import com.online.demo.service.ITContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sda1
 * @since 2020-11-30
 */
@RestController
@RequestMapping("/content")
@Slf4j
public class TContentController {
    @Autowired
    private ITContentService tcontentService;

    @PostMapping("addArticle")
    public ResultBean<TContent> addContent(@RequestParam(value = "img", required = false)MultipartFile img, TContent tcontent, HttpServletRequest request)throws Exception{
        //将标题图上传到指定位置，再把文章内容写进txt，存进指定目录，更新文章对象的文章内容为存储文件名，上传进数据库

        //首先命名文章txt
        long currentTime = System.currentTimeMillis();
        String txt_name =currentTime+".txt";
        //加载文件路径
            //暂时使用映射加载方式，如果指定或者有修改再改动
        String path = request.getSession().getServletContext().getRealPath("/extralfile");
        //创建这txt文件等待写入
        File txt_file = new File(path+"/content/txt",txt_name);
        //检查文件是否已经新建成功
        if(!txt_file.exists()){ txt_file.mkdirs(); }
        //写入并保存txt文件
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(txt_file);
            fileOutputStream.write(tcontent.getContent().getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
            tcontent.setContent(txt_name);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //将图片存进指定目录
        String[] img_name_original_spilt=img.getOriginalFilename().split(".");
        String img_name_suffix = img_name_original_spilt[img_name_original_spilt.length-1];
        String img_name_new = currentTime+"."+img_name_suffix;
        File img_new = new File(path+"/content/img",img_name_new);
        try {
            //把文件写入到磁盘中
            img.transferTo(img_new);
            //把文件写入对象中，方便数据库更新
            tcontent.setImg(img_name_new);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //添加文章的发表信息，需要从session拿到上传人，需要从系统中得到上传时间
//        Tuser user = (Tuser)request.getSession().getAttribute("Admin");
//        tcontent.setUid(user.getID());//沃日老毕user.get方法都没有，等一等
        tcontent.setCreatetime(LocalDateTime.now());//这方法新鲜，第一次用

        try {
            tcontentService.addContent(tcontent);
        }catch (Exception e){

            e.printStackTrace();
            return ResultBean.getErrorInstance("上传文章到数据库时发生错误。");
        }

        return ResultBean.getSuccessInstance("上传文章成功！");
    }

}
